package com.smalls.newvideotwo.view;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smalls.newvideotwo.R;

public class BaseDialog extends Dialog implements OnClickListener {
    private String mMessage;
    private TextView mMsgTv;
    private Button mNegativeBtn;
    private String mNegativeText;
    private OnClickListener mOnNegativeButtonClickListener;
    private OnClickListener mOnPositiveButtonClickListener;
    private Button mPositiveBtn;
    private String mPositiveText;
    private LinearLayout mRootView;

    public BaseDialog(Context context) {
        this(context, R.style.ToastDialog);
    }

    public BaseDialog(Context context, int style) {
        super(context, style);
        setContentView(R.layout.layout_base_dialog);
        this.mRootView = (LinearLayout) findViewById(R.id.base_dialog_root_view);
        this.mMsgTv = (TextView) findViewById(R.id.layout_double_dialog_msg_tv);
        this.mPositiveBtn = (Button) findViewById(R.id.layout_double_dialog_positive_btn);
        this.mNegativeBtn = (Button) findViewById(R.id.layout_double_dialog_negative_btn);
        if (!(BaseDialogConfig.getInstance().getWidth() == -1 || BaseDialogConfig.getInstance().getHeight() == -1)) {
            LayoutParams params = getWindow().getAttributes();
            params.width = BaseDialogConfig.getInstance().getWidth();
            params.height = BaseDialogConfig.getInstance().getHeight();
            getWindow().setAttributes(params);
        }
        this.mRootView.setPadding(BaseDialogConfig.getInstance().getPaddingLeft(), BaseDialogConfig.getInstance().getPaddingTop(), BaseDialogConfig.getInstance().getPaddingRight(), BaseDialogConfig.getInstance().getPaddingBottom());
        if (BaseDialogConfig.getInstance().getBackgroundResId() != -1) {
            this.mRootView.setBackgroundResource(BaseDialogConfig.getInstance().getBackgroundResId());
        }
        if (BaseDialogConfig.getInstance().getPositiveBtnBackgroundDrawableResId() != -1) {
            this.mPositiveBtn.setBackgroundResource(BaseDialogConfig.getInstance().getPositiveBtnBackgroundDrawableResId());
        }
        if (BaseDialogConfig.getInstance().getPositiveBtnTextResId() != -1) {
            this.mPositiveBtn.setText(BaseDialogConfig.getInstance().getPositiveBtnTextResId());
        }
        if (BaseDialogConfig.getInstance().getNegativeBtnBackgroundDrawableResId() != -1) {
            this.mNegativeBtn.setBackgroundResource(BaseDialogConfig.getInstance().getPositiveBtnBackgroundDrawableResId());
        }
        if (BaseDialogConfig.getInstance().getNegativeBtnTextResId() != -1) {
            this.mNegativeBtn.setText(BaseDialogConfig.getInstance().getNegativeBtnTextResId());
        }
        this.mPositiveBtn.setOnClickListener(this);
        this.mNegativeBtn.setOnClickListener(this);
    }

    public void show() {
        super.show();
        if (!(this.mMsgTv == null || TextUtils.isEmpty(this.mMessage))) {
            this.mMsgTv.setText(this.mMessage);
            this.mMsgTv.setGravity(17);
        }
        if (!(this.mPositiveBtn == null || TextUtils.isEmpty(this.mPositiveText))) {
            this.mPositiveBtn.setText(this.mPositiveText);
            this.mPositiveBtn.setVisibility(View.VISIBLE);
            this.mMsgTv.setGravity(1);
        }
        if (this.mNegativeBtn != null && !TextUtils.isEmpty(this.mNegativeText)) {
            this.mNegativeBtn.setText(this.mNegativeText);
            this.mNegativeBtn.setVisibility(View.VISIBLE);
            this.mMsgTv.setGravity(1);
        }
    }

    public void setMessage(String mMessage) {
        this.mMessage = mMessage;
    }

    public Button setPositiveButton(String mPositiveText, OnClickListener onPositiveButtonClickListener) {
        this.mPositiveText = mPositiveText;
        this.mOnPositiveButtonClickListener = onPositiveButtonClickListener;
        return this.mPositiveBtn;
    }

    public Button setNegativeButton(String mNegativeText, OnClickListener onNegativeButtonClickListener) {
        this.mNegativeText = mNegativeText;
        this.mOnNegativeButtonClickListener = onNegativeButtonClickListener;
        return this.mNegativeBtn;
    }

    public Button getmPositiveBtn() {
        return this.mPositiveBtn;
    }

    public Button getmNegativeBtn() {
        return this.mNegativeBtn;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == 23 || keyCode == 21 || keyCode == 22) {
            return super.onKeyDown(keyCode, event);
        }
        dismiss();
        cancel();
        return true;
    }

    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.layout_double_dialog_positive_btn) {
            if (this.mOnPositiveButtonClickListener != null) {
                this.mOnPositiveButtonClickListener.onClick(this, 0);
            }
        } else if (id == R.id.layout_double_dialog_negative_btn && this.mOnNegativeButtonClickListener != null) {
            this.mOnNegativeButtonClickListener.onClick(this, 1);
        }
    }
}
